package com.csun.cmny.provider.manager;

import com.csun.cmny.provider.model.domain.UacGroup;
import com.csun.cmny.provider.model.domain.UacUser;
import com.csun.cmny.provider.model.dto.properManagement.LoginNameByGroupIdDto;
import com.csun.cmny.provider.model.vo.ProperManagementLoginName;
import com.csun.cmny.provider.service.UacGroupService;
import com.csun.cmny.provider.service.UacUserService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class GroupLoginNameManager {


    @Resource
    private UacGroupService uacGroupService;
    @Resource
    private UacUserService userService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    //将机构的管理员登录名写入到定时任务
    @Scheduled(cron = "0 0 2 * * ?")
    public void selectLoginNameByGroupId() {

        List<UacGroup> uacGroups = uacGroupService.selectAll();

        for (UacGroup uacGroup : uacGroups) {
            LoginNameByGroupIdDto loginNameByGroupIdDto = new LoginNameByGroupIdDto();
            loginNameByGroupIdDto.setGroupId(uacGroup.getId());
            loginNameByGroupIdDto.setRoleId(2L);
            List<Long>useIds=uacGroupService.selectHomeUserIdByGroupId(loginNameByGroupIdDto);

            if (PublicUtil.isNotEmpty(useIds)){
                UacUser user = userService.selectByKey(useIds.get(0));
                if(PublicUtil.isNotEmpty(user)){
                    redisTemplate.opsForValue().set(RedisKeyUtil.getUacGroupLoginname(uacGroup.getId().toString()),user.getLoginName());
                }
            }
            UacGroup group = uacGroupService.selectByKey(uacGroup.getId());
            redisTemplate.opsForValue().set(RedisKeyUtil.getUacGroupName(uacGroup.getId().toString()),group.getGroupName());
        }
    }

    //将机构的管理员登录名写入到定时任务
    @Scheduled(cron = "0 0 2 * * ?")
    public void selectUserNameByUserId() {

        List<UacUser> uacUsers = userService.selectAll();

        for (UacUser uacUser : uacUsers) {
            redisTemplate.opsForValue().set(RedisKeyUtil.getUacUserName(uacUser.getId().toString()),uacUser.getUserName()+"-"+uacUser.getLoginName());

        }

    }

    }
